<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="项目ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['id']" :disabled="!isEdit" placeholder="项目ID"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectName']" placeholder="项目名称"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="项目编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectNum']" placeholder="项目编码"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectAddress']" placeholder="项目地址"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectType']" placeholder="项目类型"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="计划开工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['planBegDate']" placeholder="计划开工日期"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="计划竣工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['planEndDate']" placeholder="计划竣工日期"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="实际开工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualBeginDate']" placeholder="实际开工日期"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="实际竣工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualEndDate']" placeholder="实际竣工日期"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { getProjectById, projectEdit, projectCreate } from '../../service.js'

import JFormContainer from '@/components/jeecg/JFormContainer'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import pick from 'lodash.pick'
export default {
  name: 'ProjectForm',
  components: {
    JFormContainer,
    JDictSelectTag
  },
  props: {
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    normal: {
      type: Boolean,
      default: false,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {},
      isEdit: false
    }
  },
  computed: {
    formDisabled() {
      if (this.normal === false) {
        if (this.formData.disabled === false) {
          return false
        } else {
          return true
        }
      }
      return this.disabled
    }
  },
  created() {
    this.showFlowData()
  },
  methods: {
    add() {
      this.isEdit = false
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.isEdit = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'id',
            'projectName',
            'projectNum',
            'projectAddress',
            'projectType',
            'planBegDate',
            'planEndDate',
            'actualBeginDate',
            'actualEndDate'
          )
        )
      })
    },
    showFlowData() {
      if (this.normal === false) {
        let id = this.formData.dataId
        let params = {}
        getProjectById(id, params).then(res => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          let formData = Object.assign(this.model, values)
          let request
          if (!formData.id) {
            request = projectCreate(formData)
          } else {
            request = projectEdit(formData)
          }
          request
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
